package cn.e2win.biz.activity;

import javax.servlet.http.HttpServletRequest;

import cn.e2win.biz.comm.ViewExcel;
import cn.e2win.biz.dbo.CyzjOrg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import cn.e2win.base.controller.MyException;
import cn.e2win.base.entity.SysUser;
import cn.e2win.base.json.vo.Pager;
import cn.e2win.base.json.vo.Result;
import cn.e2win.biz.base.BaseController;
import cn.e2win.biz.base.BaseService;
import cn.e2win.biz.base.CommonController;
import cn.e2win.biz.base.CommonService;
import cn.e2win.biz.dbo.Activity;
import cn.e2win.biz.dbo.CyzjEmp;
import cn.e2win.biz.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;


@Controller
@Scope("request")
@RequestMapping("/activity")
public class ActivityController extends BaseController {
	@Autowired
	private CommonController commonController;

	@Autowired
	private ActivityService activityService;
	
	@Autowired
	private CommonService commonService;
	
	@Override
	public BaseService initService() {
		return activityService;
	}

	@RequestMapping("toActivityEditor/{id}")
	public String toActivityEditor(HttpServletRequest request, @PathVariable(ID) Integer id, ModelMap modelMap){
		Activity empty = new Activity(id);
		String clazz = Activity.class.getSimpleName();

		if(pager.getCurrUser().getUserType().equals("CyzjEmp")){
			CyzjEmp cyzjEmp = (CyzjEmp) activityService.getEntity(CyzjEmp.class,pager.getCurrUser().getEmpId());
//			modelMap.put("cyzjOrgName",cyzjEmp.getDwName());
			modelMap.put("cyzjEmp",cyzjEmp);
			CyzjOrg cyzjOrg = (CyzjOrg) activityService.getEntity(CyzjOrg.class,cyzjEmp.getOrgId());
			modelMap.put("cyzjOrg",cyzjOrg);
		}

		modelMap.put(OBJECT, activityService.getEntity(empty));
		
		modelMap.put("activityTagList", commonService.getSysLabelList(empty,"activityTag"));

		modelMap.put("allActivityTagList", commonService.getSysLabelList(clazz,"activityTag"));

		modelMap.put("poster", commonService.getSysAttachList(empty, "poster"));
		
		return "activity/activityEditor";
	}

	@RequestMapping("editActivity")
	@ResponseBody
	public Result editActivity(HttpServletRequest request, @ModelAttribute Activity obj, @RequestParam(value="delPicIds",required = false)String delPicIds, ModelMap modelMap) throws MyException{
		Result r = new Result();
		if(obj.getId()!=null) {
			commonService.delPic(delPicIds);
		}
		activityService.editActivity(obj, commonController.getDsRelationList(request,  ITEMS + "dsEmpId"), commonController.getGuestRelationList(request, "guest."));
		r.setCallbackType(Result.CLOSE_CURRENT);
		r.setNavTabId("listView_"+modelMap.get(FUNID));
		return r;
	}




	@RequestMapping("activityPager")
	public String activityPager(HttpServletRequest request, ModelMap modelMap){
		/*if("APPROVE".equals(todo)){
			pager.getScMap().put("approveStatus", "SUBMITED");
		}else if("EDIT".equals(todo)){
			pager.getScMap().put("approveStatus_IN", "UNSUBMIT【】REJECTED");
			
			if("CyzjEmp".equals(pager.getCurrUser().getUserType())){
				pager.getScMap().put("orgId",pager.getCurrUser().getOrgId()+"");
				pager.getScMap().put("createById",pager.getCurrUser().getId()+"");
			}else if("CyzjEmp".equals(pager.getCurrUser().getUserType())){
				pager.getScMap().put("createById",pager.getCurrUser().getId()+"");
			}
		}else if("fieldCtrl".equals(todo)){

		}else if("fileMgr".equals(todo)){
		
		}else if("evaluateMgr".equals(todo)){
		
		}*/

		setScMap(pager,todo);


		activityService.pagerActivity(pager);
		modelMap.put("pager", pager);
		return "activity/activityPager";
	}

	@RequestMapping("activityPagerExcel")
	public ModelAndView customerPagerExcel(HttpServletRequest request, ModelMap modelMap) {
		pager.setReturnAll(true);
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("key",activityPager(request,modelMap));
		model.put("list", pager.getResultList());
		return new ModelAndView(new ViewExcel(), model);
	}

	private void setScMap(Pager pager, String todo){

		SysUser user = pager.getCurrUser();
		String user_I = pager.getScMap().get("user_I");
		
		//我的工作区
		if(!StringUtils.isEmpty(user_I)){
			//外部用户，导师及创业之家用户
			if(user.getUserType().equals("DsEmp") || user.getUserType().equals("CyzjEmp")){
				//领导
				if("1".equals(user.getIsLeader())){
					
				//普通工作人员
				}else{
				}
			//内部用户，即指导中心用户	
			}else if(user.getUserType().equals("ZdzxEmp")){
				//领导
				if("1".equals(user.getIsLeader())){
					pager.getScMap().remove("user_I");
				//普通工作人员
				}else{
					
				}
			}
		//非我的工作区
		}else{
			//外部用户，导师及创业之家用户
			if(user.getUserType().equals("DsEmp") || user.getUserType().equals("CyzjEmp")){
				//领导
				if("1".equals(user.getIsLeader())){
					
				//普通工作人员
				}else{
					
				}
				if("EDIT".equals(todo)){
					if("CyzjEmp".equals(user.getUserType())){
						pager.getScMap().put("orgId",pager.getCurrUser().getOrgId()+"");
						//pager.getScMap().put("createById",user.getId()+"");
					}else if("CyzjEmp".equals(pager.getCurrUser().getUserType())){
						pager.getScMap().put("createById",user.getId()+"");
					}
				}else{
				
					pager.getScMap().put("approveStatus", "APPROVED");
				}
			//内部用户，即指导中心用户	
			}else if(user.getUserType().equals("ZdzxEmp")){
				//领导
				if("1".equals(user.getIsLeader())){
				//普通工作人员
				}else{
					//资格审核，资料更新审核， 资料修改
					if("APPROVE".equals(todo) || "MDF_APPROVE".equals(todo)  || "UPDATE".equals(todo)){
						pager.getScMap().put("adminUserId", user.getId()+"");
					//修改
					}else if("EDIT".equals(todo)){
						pager.getScMap().put("createById", user.getId()+"");
					}
				}
			}
			
			if("APPROVE".equals(todo)){
				pager.getScMap().put("approveStatus", "SUBMITED");
			}else if("EDIT".equals(todo)){
					pager.getScMap().put("approveStatus_IN", "UNSUBMIT【】REJECTED");
			}
					
		}

		
	}


}
